package com.example.administrator.zeycar.Util.io;

import android.os.Bundle;

import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/10/17 0017.
 */

public class Type2Utils {

    public static final InputStream Byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }

    public static final byte[] Input2Byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    public JSONObject Map2Json(Map<String, Object> dataMap) {
        JSONObject json = new JSONObject();
        try {
            if (dataMap != null) {
                Iterator iter = dataMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String key = entry.getKey().toString();
                    String val = entry.getValue() != null ? entry.getValue().toString() : "";
                    json.put(key, val);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public String Map2Str(Map map) {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String key = entry.getKey().toString();
                String val = entry.getValue() != null ? entry.getValue().toString() : "";
                stringBuffer.append("key : " + key + " val : " + val + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    public Map StrToMap(String s) {
        Map map = new HashMap();
        try {
            if (s != null && s.trim().length() > 0) {
                JSONObject json = new JSONObject(s);
                Iterator keys = json.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = json.get(key).toString();
                    if (value.startsWith("{") && value.endsWith("}")) {
                        map.put(key, StrToMap(value));
                    } else {
                        map.put(key, value);
                    }
                }
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public String Bundle2Str(Bundle bundle) {
        String contStr = "";
        if (bundle != null) {
            Set<String> keySet = bundle.keySet();
            for (String key : keySet) {
                Object value = bundle.get(key);
                contStr += "key : " + key + " value : " + value + "\n";
            }
        }
        return contStr;
    }
}
